#ifndef _NETWORK_H
#define _NETWORK_H


#include "socket.h"
#include "http.h"
#include "saip_net.h"

#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus */


/**
   \defgroup network Network
 
  @section network_intro Introduction

  Network contains some sub module such as socket, http and some common APIs.
  Detailed description for socket/http are not documented here, please refer the sub
  modules document.

  @section network_session Typical session

  The steps to work with Network module:
  - call Network_Initialize to initialize the network // only once before anyother network operatiron (Framework has called for you).
  - \ref network_setup
  - \ref network_ghn
  - create your socket or open a http socket
  - Network_Terminate //only once before exit application (Framework has called for you).
 
  @section network_setup To setup the wireless connection

  Before connect a socket to server we should setup the  wireless network connection first.
  Two platform APIs for this purpose:
  \code
  typedef int32 (*MR_INIT_NETWORK_CB)(int32 result);  
  int32 mrc_initNetwork(MR_INIT_NETWORK_CB cb, const char *mode);
  \endcode
  The detailed description for these two APIs will not documented here please refer "mrc_base.h".

  Sample code:
  \code
	//the callback function
	int32 OnNetworkSetup(int32 result)
	{
		if(result == MR_FAILED){
			//error handle
		}else if(result == MR_SUCCESS){
			//network setup success
		}

		//if MR_WAITING do nothing
		return result;
	}

	//setup code
	//for mrc_initNetwork maybe return immediately
	OnNetworkSetup(mrc_initNetwork(OnNetworkSetup, "CMNET"));
  \endcode
  	
  @section network_ghn To get the IP from a host name

  For socket/http interface just use IP address to connect the server,  you must get the ip first 
  when you only have the host name.
  Two platform APIs for this purpose:
  \code
  typedef int32 (*MR_GET_HOST_CB)(int32 ip);
  int32 mrc_getHostByName(const char *name, MR_GET_HOST_CB cb);
  \endcode
  The detailed description refer to "mrc_base.h".

  Sample code:
  \code
  	//the callback function
  	int32 OnGetHostname(int32 ip)
  	{
  		if(ip == MR_WAITING)
  		{	
			//do nothing  			
  		}else if(ip == MR_FAILED){
  			//error handle
  		}else{
  			//get the ip success
  		}

  		return ip;
  	}

  	//your code
  	//for mrc_getHostByName maybe return immediately
  	OnGetHostname(mrc_getHostByName(OnGetHostname, "www.xxx.com"));
  \endcode
  
   \sa Network_Initialize, Network_Terminate
  
   @{
 */

/**
 * \brief Max host name length.
 *
 * As example: "www.baidu.com" is the host name for http://www.baidu.com.
 * Reconfigure this value when needed.
 */
#ifndef MAX_HOSTNAME_LEN
#define MAX_HOSTNAME_LEN		112
#endif


/**
 * \brief  Initialize the Network Module.
 *
 * This is just a wrapper function for sub modules initialization, and called by the Framework
 * before InitApplication, so just forget this function and all the network sub module intializations 
 * and terminations, otherwise maybe cause some unknow issues.
 *
 * \sa InitApplication, Network_Terminate
 */
VOID Network_Initialize(VOID);

/**
 * \brief Terminate the network module
 *
 * When the network module not needed by the application, call this function to terminate the the network module.
 * Framework has called this function for you, so just forget it.
 *
 * \sa Network_Initialize
 */
VOID Network_Terminate(VOID);

/**
 * \brief Converts a string containing an (Ipv4) Internet Protocol dotted address into network byte order ip address 
 *
 * \param cp A null-terminated character string representing a number expressed in the Internet standard ".'' (dotted) notation. 
 * \return the ip address in TCP/IP network byte order.
 */
Uint32 inet_addr(PCSTR cp);

/**
 * \brief Converts an (Ipv4) Internet network address into a string in Internet standard dotted format
 *
 * \param ip the ip address in network byte order. 
 * \return a char pointer to a static buffer containing the text address in standard ".'' notation. Otherwise, it returns NULL.
 */
PCSTR inet_ntoa(Uint32 ip);

/**
 * \brief Get the host name and port number from url.
 *
 * \param url the url to be parsed as format "http://www.sina.com" and the protocol field must exists.
 * \param[out] port the return port number
 * \param def the default port number when no port found in the url
 * \return the host name
 */
PCSTR host(PCSTR url, Uint16* port, Uint16 def);

/** @} end of network */

#ifdef __cplusplus
}
#endif  /* __cplusplus */

#endif //_NETWORK_H
